package com.yunsoft.gateway.interceptor.handler;


import com.yunsoft.gateway.annotation.Resubmit;
import com.yunsoft.gateway.annotation.WebInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;

/**
 * 防止重复提交拦截器
 * @author summerrains
 *
 */
@WebInterceptor(name = "resubmitHandlerInterceptor", pathPatterns = { "/**" }, order = 3)
public class ResubmitHandlerInterceptor extends BaseHandlerInterceptor {

	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if(check(handler)) {
			return true;
		}
		return false;
	}
	
	/**
     * 是否有权限
     *
     * @param handler
     * @return
     */
    private boolean check(Object handler) {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 获取方法上的注解
            Resubmit resubmit = handlerMethod.getMethod().getAnnotation(Resubmit.class);
            // 如果方法上的注解为空 则获取类的注解
            if (resubmit == null) {
            	resubmit = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Resubmit.class);
            }
            // 如果标记了注解，则判断权限
            if (resubmit != null) {
                // redis或数据库 中获取该用户的权限信息 并判断是否有权限
            }
        }
        return true;
    }
}
